function meta_analysis_percent_mismatch_cells(proj_meta,cur_sites)

% cur_sites=[2 4];
[shad_all,~,~]=meta_analysis_get_exp_conditions(proj_meta,cur_sites);
[all_act]=meta_analysis_get_act_array(proj_meta,cur_sites);

mean_act=zeros(976,4,7);
val=zeros(976,7);
all_sums=[];
cnt=0;
for und=1:2
    for tnd=1:7
        cur_act=all_act{tnd,und};
        if ~isempty(cur_act)
            cur_act(:,end-4999:end)=[];
            cur_act=cur_act-1;
            cell_thresh=[];
            for snd=1:size(cur_act,1)
                cur_trace=cur_act(snd,:);
                cell_thresh(snd,1)=std(cur_trace(cur_trace<median(cur_trace)))*4.75;
                val(cnt+snd,tnd,1)=std(cur_trace(cur_trace<1));
                val(cnt+snd,tnd,2)=cell_thresh(snd,1);
            end
            aaa=repmat(cell_thresh,1,size(cur_act,2));
            ddd=cur_act>aaa;
            cur_act(~ddd)=0;
            sums=[];
            for snd=1:size(cur_act,1)
                count=0;
                cur_trace=cur_act(snd,:);
                cur_ids=find(diff(logical(cur_trace))==1)+1;
                cur_ids((cur_ids+9)>length(cur_trace))=[];
                if ~isempty(cur_ids)
                    for hnd=1:length(cur_ids)
                        if sum(logical(cur_trace(cur_ids(hnd):cur_ids(hnd)+9)))==10
                            count=count+1;
                        end
                    end
                end
                sums(snd,1)=count;
            end
            all_sums(cnt+1:cnt+size(cur_act,1),tnd)=sums;
            ids=find(sums>0);
            for jnd=1:4
                mean_act(ids+cnt,jnd,tnd)=mean(cur_act(ids,shad_all{tnd,und}==jnd-1),2);
            end
        end
    end
    cnt=cnt+size(all_act{1,und},1);
end

cond_cells=zeros(size(mean_act,1),size(mean_act,2),size(mean_act,3));
for tnd=1:7
    for hnd=1:size(mean_act,1)
        if sum(mean_act(hnd,:,tnd))~=0
            cur_row=mean_act(hnd,:,tnd);
            [a,b]=max(cur_row);
            cur_row(b)=[];
            others=max(cur_row);
            if (others*3.5)<a
                cond_cells(hnd,b,tnd)=a;
            end
        end
    end
end

% cells=find(logical(cond_cells(:,4,1)));
    
col{1}=[0.8 0.8 0.8];
col{2}=[0.3 0.7 0.3];
col{3}=[1 0.5 0];
col{4}=[1 1 0.5];
col{5}=[0.9 0.2 0.2];
col{6}=[0 1 1];
col{7}=[1 0.5 0];
% 
% for tnd=1:10
%     figure;
%     set(gcf,'pos',[50         616        3418         161])
%     for hnd=1:7
%         area([1:length(shad_all{1,1})],4.5*(shad_all{1,1}==hnd),'facecolor',col{hnd},'linestyle','none')
%         hold on
%     end
%     plot(all_act{1,1}(cells(tnd),:),'k')
%     xlim([0 size(all_act{1,1},2)])
% end

figure;
for tnd=1:3
    nums=(sum(logical(cond_cells(1:574,:,tnd)),1)./976)*100;
    scatter(tnd,nums(1),60,[0,0,0],'fill')
    hold on
    scatter(tnd,nums(2),60,col{1},'fill')
    scatter(tnd,nums(3),60,col{2},'fill')
    scatter(tnd,nums(4),60,col{3},'fill')
    legend('baseline','feedback','playback','mismatch')
    xlim([0 5])
    xlabel('time points')
    ylabel('% cells')
end
% 
% figure;
% for tnd=1:7
%     nums=(sum(logical(cond_cells(1:574,:,tnd)),1)./574)*100;
%     scatter(tnd,nums(1),60,[0 0 0],'fill')
%     hold on
%     scatter(tnd,nums(2),60,col{1},'fill')
%     scatter(tnd,nums(3),60,col{2},'fill')
%     scatter(tnd,nums(4),60,col{3},'fill')
%     xlim([0 7])
% end
% 
% figure;
% hold on
% for tnd=1:3
%     scatter(tnd,median(val(:,tnd,1)),50,[0 0 0])
% end

% 
% 
% 
% 
% fb_snip={};
% pb_snip={};
% fb_av={};
% pb_av={};
% ini_cnt_2=1;
% for ond=1:length(cur_sites)
%     cur_site=cur_sites(ond);
%     all_act={};
%     for tnd=1:size(proj_meta(cur_site).rd,2)
%         cnt=0;
%         for vnd=1:size(proj_meta(cur_site).rd,1)
%             all_act{tnd}(cnt+1:cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1),:)=proj_meta(cur_site).rd(vnd,tnd).act;
%             cnt=cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1);
%         end
%     end
%     for tnd=1:size(proj_meta(cur_site).rd,2)
%         nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
%         stim_id=proj_meta(cur_site).rd(1,tnd).ps_id;
%         runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.005;
%         runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
%         runM_raw=proj_meta(cur_site).rd(1,tnd).velM_smoothed;
%         ini_cnt2=ini_cnt_2;
%         for lnd=1:size(all_act{1},1)
%             act=all_act{tnd}(lnd,:);
%             [fb_av{ini_cnt2,tnd},pb_av{ini_cnt2,tnd},fb_snip{ini_cnt2,tnd},pb_snip{ini_cnt2,tnd}]=pert_ana_meta(nbr_frames,stim_id,runM,runP,runM_raw,act);
%             ini_cnt2=ini_cnt2+1;
%         end
%     end
%     ini_cnt_2=ini_cnt2;
%     %     [vis_snips,run_snips,ffm_snips,fdbk_snips]=meta_analysis_find_onset_snips(proj_meta,cur_site,vis_fl_all,order,exp_inds,shad_all);
%     %     [blink_snips,blink_snips_dark,eye_snips,eye_snips_dark]=meta_analysis_eye_onset(proj_meta,cur_site,order,exp_inds,shad_all);
% end
% 
% LFM_res=[];
% for tnd=1:size(fb_av,2)
%     for jnd=1:size(fb_av,1)
%         if ~isempty(fb_av{jnd,tnd})
%             for hnd=1:13
%                 if ~isempty(fb_av{jnd,tnd}{hnd})
%                     LFM_res(jnd,hnd,tnd)=mean(fb_av{jnd,tnd}{hnd}(11:20));
%                 else
%                     LFM_res(jnd,hnd,tnd)=0;
%                 end
%             end
%             for hnd=1:13
%                 if ~isempty(pb_av{jnd,tnd}{hnd})
%                     LFM_res(jnd,hnd+13,tnd)=mean(pb_av{jnd,tnd}{hnd}(11:20));
%                 else
%                     LFM_res(jnd,hnd,tnd)=0;
%                 end
%             end
%         else
%             LFM_res(jnd,1:26,tnd)=0;
%         end
%     end
% end
% 
% thresh=3;
% [~,b]=max(LFM_res(:,:,1),[],2);
% res_arr=zeros(size(LFM_res,1),size(LFM_res,2));
% for hnd=1:size(LFM_res,1)
%     cur_row=LFM_res(hnd,:,1);
%     cur_row=cur_row*100;
%     cur_row=cur_row-100;
%     cur_row(cur_row<0)=0;
%     cur_max=cur_row(b(hnd));
%     cur_row(b(hnd))=0;
%     cur_row=cur_row*thresh;
%     if sum(cur_row<cur_max)==length(cur_row)
%         res_arr(hnd,b(hnd))=1;
%     elseif sum(cur_row<cur_max)==length(cur_row)-1
%         old_res=(cur_row<cur_max);
%         second_ind=find(old_res==0);
%         cur_row=LFM_res(hnd,:,1);
%         cur_row=cur_row*100;
%         cur_row=cur_row-100;
%         cur_row(cur_row<0)=0;
%         cur_row(b(hnd))=0;
%         second_max=cur_row(second_ind);
%         cur_row(second_ind)=0;
%         cur_row=cur_row*thresh;
%         if sum(cur_row<second_max)==length(cur_row)
%             res_arr(hnd,b(hnd))=1;
%             res_arr(hnd,second_ind)=1;
%         end
%     elseif sum(cur_row<cur_max)==length(cur_row)-2
%         old_res=(cur_row<cur_max);
%         second_ind=find(old_res==0);
%         cur_row=LFM_res(hnd,:,1);
%         cur_row=cur_row*100;
%         cur_row=cur_row-100;
%         cur_row(cur_row<0)=0;
%         cur_row(b(hnd))=0;
%         second_max=cur_row(second_ind);
%         cur_row(second_ind)=0;
%         cur_row=cur_row*thresh;
%         yes=[];
%         for fnd=1:2
%             if sum(cur_row<second_max(fnd))==length(cur_row)
%                 yes(fnd)=1;
%             else
%                 yes(fnd)=0;
%             end
%         end
%         if sum(yes)==2
%             res_arr(hnd,b(hnd))=1;
%             res_arr(hnd,second_ind)=1;
%         end
%     elseif sum(cur_row<cur_max)==length(cur_row)-3
%         old_res=(cur_row<cur_max);
%         second_ind=find(old_res==0);
%         cur_row=LFM_res(hnd,:,1);
%         cur_row=cur_row*100;
%         cur_row=cur_row-100;
%         cur_row(cur_row<0)=0;
%         cur_row(b(hnd))=0;
%         second_max=cur_row(second_ind);
%         cur_row(second_ind)=0;
%         cur_row=cur_row*thresh;
%         yes=[];
%         for fnd=1:3
%             if sum(cur_row<second_max(fnd))==length(cur_row)
%                 yes(fnd)=1;
%             else
%                 yes(fnd)=0;
%             end
%         end
%         if sum(yes)==3
%             res_arr(hnd,b(hnd))=1;
%             res_arr(hnd,second_ind)=1;
%         end
%     elseif sum(cur_row<cur_max)==length(cur_row)-4
%         old_res=(cur_row<cur_max);
%         second_ind=find(old_res==0);
%         cur_row=LFM_res(hnd,:,1);
%         cur_row=cur_row*100;
%         cur_row=cur_row-100;
%         cur_row(cur_row<0)=0;
%         cur_row(b(hnd))=0;
%         second_max=cur_row(second_ind);
%         cur_row(second_ind)=0;
%         cur_row=cur_row*thresh;
%         yes=[];
%         for fnd=1:4
%             if sum(cur_row<second_max(fnd))==length(cur_row)
%                 yes(fnd)=1;
%             else
%                 yes(fnd)=0;
%             end
%         end
%         if sum(yes)==4
%             res_arr(hnd,b(hnd))=1;
%             res_arr(hnd,second_ind)=1;
%         end
%     end
% end
% 
% cells=find(sum(res_arr,2)==5)'
